package com.cnyao.phot;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

/*
 * LoginActivity is the launcher Activity for this application.
 * - It need a view set in setContentView() to show. Which should
 *   define in layout/ folder.
 * - It need to get the exist user information from the database.
 * - It need to get the new user information from Internet.
 * - It has 2 Menus. And define some menu operations.
 * - The Activity extends Activity, and implements 2 interfaces.
 * 
 * Step1: create LoginActivity.
 * Step2: create loginview.xml in layout/ folder.
 * */
public class LoginActivity extends Activity 
		implements AdapterView.OnItemClickListener{
	
	private SQLiteDatabase mDatabase;
	private UserAdapter mAdapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //set the database
        mDatabase=new UserDatabase(this).getWritableDatabase();
        setContentView(R.layout.loginview);
        
        //get the adapter for listview
        
        //set up the view
        mAdapter=new UserAdapter(this, initializeCursor());
        final ListView userList=(ListView)findViewById(R.id.userlist);
        userList.setAdapter(mAdapter);
        userList.setOnItemClickListener(this);
        
        /*
        Animation listanim=AnimationUtils.loadAnimation(this, R.anim.listanimation2);
        listanim.setInterpolator(AnimationUtils.loadInterpolator(this, 
        		android.R.anim.bounce_interpolator));
        userList.startAnimation(listanim);
        */
        
        DisplayMetrics dispMetr=new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dispMetr);
        int width=dispMetr.widthPixels;
        Animation listanim=new TranslateAnimation(width,
        		0.0f,
        		0.0f,0.0f);
        listanim.setDuration(1000);
        listanim.setStartOffset(300);
        listanim.setRepeatMode(Animation.RESTART);
        //listanim.setRepeatCount(Animation.INFINITE);
        listanim.setInterpolator(AnimationUtils.loadInterpolator(this, 
        		android.R.anim.bounce_interpolator));
        userList.startAnimation(listanim);
    }

	private Cursor initializeCursor() {
		Cursor cursor=mDatabase.query("users",
				new String[]{UserDatabase._ID, "realname", "nsid", "buddy_icon"},
				null, null, null, null, "username ASC");
		startManagingCursor(cursor);
		return cursor;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Here only the Toast, later will go to another activity.
		Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
	}
}